package com.ebelter.myehc.models.http.request;


import com.ebelter.btcomlib.models.bluetooth.products.bracelet.bean.RateBean;
import com.ebelter.btcomlib.models.bluetooth.products.bracelet.bean.SleepArrayInfo;

import com.ebelter.btcomlib.models.bluetooth.products.bracelet.bean.WalkBean;
import com.ebelter.myehc.models.http.constant.HttpConstant;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ebelter.myehc.models.http.constant.HttpConstant.RequestField.bizId;
import static com.ebelter.myehc.models.http.constant.HttpConstant.RequestField.funcId;
import static com.ebelter.myehc.models.http.constant.HttpConstant.RequestField.version;

class EhcJsonUtil {


    String getUpLoadTemperatureJS(String authKey, float temperature, String time) {

        Map<String, Serializable> paramMap = new HashMap<>();
        paramMap.put(HttpConstant.RequestField.temperature, temperature);
        paramMap.put(HttpConstant.RequestField.time, time);
        String dataJs = getMapRJ(paramMap);

        paramMap.clear();
        paramMap.put(bizId, "08");
        paramMap.put(funcId, "02");
        paramMap.put(version, "01");
        paramMap.put(HttpConstant.RequestField.authKey, authKey);
        paramMap.put(HttpConstant.RequestField.data, dataJs);

        return getMapRJ(paramMap);
    }

    String getUpLoadBloodOxygenJS(String authKey, float bloodoxygen, int pulse, String time) {

        Map<String, Serializable> paramMap = new HashMap<>();
        paramMap.put(HttpConstant.RequestField.bloodoxygen, bloodoxygen);
        paramMap.put(HttpConstant.RequestField.pulse, pulse);
        paramMap.put(HttpConstant.RequestField.time, time);
        String dataJs = getMapRJ(paramMap);

        paramMap.clear();
        paramMap.put(bizId, "08");
        paramMap.put(funcId, "04");
        paramMap.put(version, "01");
        paramMap.put(HttpConstant.RequestField.authKey, authKey);
        paramMap.put(HttpConstant.RequestField.data, dataJs);

        return getMapRJ(paramMap);
    }


    //拼凑所有的键值对json信息
    private String getMapRJ(Map<String, Serializable> paramMap) {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        if (paramMap != null) {
            for (String key : paramMap.keySet()) {
                sb.append("\"").append(key).append("\":");
                Serializable value = paramMap.get(key);
                if (value instanceof String) {
                    if (HttpConstant.RequestField.data.equals(key)) {
                        sb.append((String) value);
                    } else {
                        sb.append("\"").append((String) value).append("\"");
                    }

                } else if (value instanceof Integer) {
                    sb.append(value);
                } else if (value instanceof Float) {
                    sb.append(value);
                } else if (value instanceof Long) {
                    sb.append(value);
                }
                sb.append(",");
            }
            sb.deleteCharAt(sb.lastIndexOf(","));
        }

        sb.append("}");

        return sb.toString();
    }

    String getLoadBloodSugarJS(String authKey, float bloodsugar, int type, String time) {

        Map<String, Serializable> paramMap = new HashMap<>();
        paramMap.put(HttpConstant.RequestField.bloodsugar, bloodsugar);
        paramMap.put(HttpConstant.RequestField.type, type);
        paramMap.put(HttpConstant.RequestField.time, time);
        String dataJs = getMapRJ(paramMap);

        paramMap.clear();
        paramMap.put(bizId, "08");
        paramMap.put(funcId, "05");
        paramMap.put(version, "01");
        paramMap.put(HttpConstant.RequestField.authKey, authKey);
        paramMap.put(HttpConstant.RequestField.data, dataJs);

        return getMapRJ(paramMap);
    }


    private String getPageJS(String bizId, String funcId, String version, String authKey, int pageNo, int pageSize) {
        Map<String, Serializable> paramMap = new HashMap<>();
        paramMap.put(HttpConstant.RequestField.pageNo, pageNo);
        paramMap.put(HttpConstant.RequestField.pageSize, pageSize);
        String dataJs = getMapRJ(paramMap);

        paramMap.clear();
        paramMap.put(HttpConstant.RequestField.bizId, bizId);
        paramMap.put(HttpConstant.RequestField.funcId, funcId);
        paramMap.put(HttpConstant.RequestField.version, version);
        paramMap.put(HttpConstant.RequestField.authKey, authKey);
        paramMap.put(HttpConstant.RequestField.data, dataJs);

        return getMapRJ(paramMap);
    }

    String getBloodSugarPageJS(String authKey, int pageNo, int pageSize) {
        return getPageJS("08", "114", "01", authKey, pageNo, pageSize);

    }

    String getLoadBloodPressureJS(String authKey, int systolic, int diastolic, int pulse, String time) {
        Map<String, Serializable> paramMap = new HashMap<>();
        paramMap.put(HttpConstant.RequestField.systolic, systolic);
        paramMap.put(HttpConstant.RequestField.diastolic, diastolic);
        paramMap.put(HttpConstant.RequestField.pulse, pulse);
        paramMap.put(HttpConstant.RequestField.time, time);
        String dataJs = getMapRJ(paramMap);

        paramMap.clear();
        paramMap.put(bizId, "08");
        paramMap.put(funcId, "06");
        paramMap.put(version, "01");
        paramMap.put(HttpConstant.RequestField.authKey, authKey);
        paramMap.put(HttpConstant.RequestField.data, dataJs);

        return getMapRJ(paramMap);
    }

    String getLoadCholesterolJS(String authKey, float cholesterol, String time) {
        Map<String, Serializable> paramMap = new HashMap<>();
        paramMap.put(HttpConstant.RequestField.cholesterol, cholesterol);
        paramMap.put(HttpConstant.RequestField.time, time);
        String dataJs = getMapRJ(paramMap);

        paramMap.clear();
        paramMap.put(bizId, "08");
        paramMap.put(funcId, "16");
        paramMap.put(version, "01");
        paramMap.put(HttpConstant.RequestField.authKey, authKey);
        paramMap.put(HttpConstant.RequestField.data, dataJs);

        return getMapRJ(paramMap);
    }

    //上传体脂成分
    String getLoadBodyCompositionJS(String authKey, float weight, float adiposerate, float moisture, int basalMetabolism, float visceralfat, float muscle, float bone, float bmi, float protein, String time) {

        Map<String, Serializable> paramMap = new HashMap<>();
        paramMap.put(HttpConstant.RequestField.weight, weight);
        paramMap.put(HttpConstant.RequestField.adiposerate, adiposerate);
        paramMap.put(HttpConstant.RequestField.moisture, moisture);
        paramMap.put(HttpConstant.RequestField.basalMetabolism, basalMetabolism);
        paramMap.put(HttpConstant.RequestField.visceralfat, visceralfat);
        paramMap.put(HttpConstant.RequestField.muscle, muscle);
        paramMap.put(HttpConstant.RequestField.bone, bone);
        paramMap.put(HttpConstant.RequestField.bmi, bmi);
        paramMap.put(HttpConstant.RequestField.protein, protein);
        paramMap.put(HttpConstant.RequestField.time, time);
        String dataJs = getMapRJ(paramMap);

        paramMap.clear();
        paramMap.put(bizId, "08");
        paramMap.put(funcId, "07");
        paramMap.put(version, "03");
        paramMap.put(HttpConstant.RequestField.authKey, authKey);
        paramMap.put(HttpConstant.RequestField.data, dataJs);

        return getMapRJ(paramMap);


    }

    //获取血氧page
    String getBloodOxygenPageJS(String authKey, int pageNo, int pageSize) {
        return getPageJS("08", "112", "01", authKey, pageNo, pageSize);

    }

    String getBloodPressurePageJS(String authKey, int pageNo, int pageSize) {
        return getPageJS("08", "113", "01", authKey, pageNo, pageSize);
    }

    String getBodyCompositionPageJS(String authKey, int pageNo, int pageSize) {
        return getPageJS("08", "115", "01", authKey, pageNo, pageSize);
    }

    String getCholesterolPageJS(String authKey, int pageNo, int pageSize) {
        return getPageJS("08", "116", "01", authKey, pageNo, pageSize);
    }

    String getTemperaturePageJS(String authKey, int pageNo, int pageSize) {
        return getPageJS("08", "117", "01", authKey, pageNo, pageSize);
    }

    String getDeviceReportDataJS(String authKey, int measureType, int dateType, String param) {

        Map<String, Serializable> paramMap = new HashMap<>();
        paramMap.put(HttpConstant.RequestField.measureType, measureType);
        paramMap.put(HttpConstant.RequestField.dateType, dateType);
        paramMap.put(HttpConstant.RequestField.param, param);
        String dataJs = getMapRJ(paramMap);

        paramMap.clear();
        paramMap.put(bizId, "08");
        paramMap.put(funcId, "118");
        paramMap.put(version, "01");
        paramMap.put(HttpConstant.RequestField.authKey, authKey);
        paramMap.put(HttpConstant.RequestField.data, dataJs);

        return getMapRJ(paramMap);
    }

    String getLoadUaJS(String authKey, int ua, String time) {
        Map<String, Serializable> paramMap = new HashMap<>();
        paramMap.put(HttpConstant.RequestField.uricacid, ua);
        paramMap.put(HttpConstant.RequestField.time, time);
        String dataJs = getMapRJ(paramMap);

        paramMap.clear();
        paramMap.put(bizId, "08");
        paramMap.put(funcId, "18");
        paramMap.put(version, "01");
        paramMap.put(HttpConstant.RequestField.authKey, authKey);
        paramMap.put(HttpConstant.RequestField.data, dataJs);

        return getMapRJ(paramMap);
    }



    String getUAPageJS(String authKey, int pageNo, int pageSize) {
        return getPageJS("08", "119", "01", authKey, pageNo, pageSize);
    }

    String getLoginJs(String username, String psw) {
        Map<String, Serializable> paramMap = new HashMap<>();
        paramMap.put("grant_type", "password");
        paramMap.put("client_id", 2);
        paramMap.put("client_secret", "XsGyrtELCSDxDqKHIkIq4eLx1FEg2ApLJQ8Rzt4R");
        paramMap.put("username", username);
        paramMap.put("password", psw);
        return getMapRJ(paramMap);
    }

    String getQueryUserIsExitByIdJS(String userId) {
        Map<String, Serializable> paramMap = new HashMap<>();
        paramMap.put("userId", userId);
        String dataJs = getMapRJ(paramMap);
        paramMap.clear();
        paramMap.put(bizId, "01");
        paramMap.put(funcId, "02");
        paramMap.put(version, "22");
        paramMap.put(HttpConstant.RequestField.data, dataJs);

        return getMapRJ(paramMap);
    }

    String getRegisterJS(String name, String userName, String sex, String birthday,
                         String height, String weight, String dtype, String softver,
                         String userId, String sn, String pwd) {

        Map<String, Serializable> paramMap = new HashMap<>();
        paramMap.put("name", name);
        paramMap.put("userName", userName);
        paramMap.put("sex", sex);
        paramMap.put("birthday", birthday);
        paramMap.put("height", height);
        paramMap.put("weight", weight);
        paramMap.put("dtype", dtype);
        paramMap.put("softver", softver);
        paramMap.put("userId", userId);
        paramMap.put("sn", sn);
        paramMap.put("pwd", pwd);
        String dataJs = getMapRJ(paramMap);
        paramMap.clear();
        paramMap.put(bizId, "01");
        paramMap.put(funcId, "01");
        paramMap.put(version, "21");
        paramMap.put(HttpConstant.RequestField.authKey, "");
        paramMap.put(HttpConstant.RequestField.data, dataJs);

        return getMapRJ(paramMap);

    }

    String getUpdatePersonInfo(Map<String, Serializable> paramMap, String authKey){
        String dataJs = getMapRJ(paramMap);
        paramMap.clear();
        paramMap.put(bizId, "08");
        paramMap.put(funcId, "01");
        paramMap.put(version, "11");
        paramMap.put(HttpConstant.RequestField.authKey, authKey);
        paramMap.put(HttpConstant.RequestField.data, dataJs);
        return getMapRJ(paramMap);
    }

    //获取上传心率的json
    String getLoadXinLvJS(String authKey, List<RateBean> lists) {
        if (lists==null)return "";

        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for(RateBean rateBean:lists){

            Map<String, Serializable> paramMap = new HashMap<>();
            paramMap.put("heartCount",rateBean.rate+"");
            paramMap.put("time",rateBean.timeStr);
            String dataJs = getMapRJ(paramMap);
            sb.append(dataJs).append(",");
        }
        sb.deleteCharAt(sb.lastIndexOf(","));
        sb.append("]");
        Map<String, Serializable> paramMap2 = new HashMap<>();
        paramMap2.put(bizId, "08");
        paramMap2.put(funcId, "38");
        paramMap2.put(version, "01");
        paramMap2.put(HttpConstant.RequestField.authKey, authKey);
        paramMap2.put(HttpConstant.RequestField.data, sb.toString());
        return getMapRJ(paramMap2);
    }


    String getLoadStepsDistanceCariliJS(String authKey, List<WalkBean> lists) {
        if (lists==null)return "";
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for(WalkBean walkBean:lists){
            Map<String, Serializable> paramMap = new HashMap<>();
            paramMap.put("hourSteps",walkBean.houreSteps+"");
            paramMap.put("hourDistances",walkBean.houreDistance+"");
            paramMap.put("hourCalories",walkBean.houreCalories+"");
            paramMap.put("time",walkBean.timeStr);

            String dataJs = getMapRJ(paramMap);
            sb.append(dataJs).append(",");
        }
        sb.deleteCharAt(sb.lastIndexOf(","));
        sb.append("]");
        Map<String, Serializable> paramMap2 = new HashMap<>();
        paramMap2.put(bizId, "08");
        paramMap2.put(funcId, "39");
        paramMap2.put(version, "01");
        paramMap2.put(HttpConstant.RequestField.authKey, authKey);
        paramMap2.put(HttpConstant.RequestField.data, sb.toString());
        return getMapRJ(paramMap2);
    }

    //上传睡眠的信息
    String getLoadSleepJS(String authKey, List<SleepArrayInfo> sleepArrayInfos) {
        if (sleepArrayInfos==null)return "";
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for(SleepArrayInfo sleepArrayInfo:sleepArrayInfos){

            Map<String, Serializable> paramMap = new HashMap<>();
            paramMap.put("startTime",sleepArrayInfo.startTime);
            paramMap.put("endTime",sleepArrayInfo.endTime);
            paramMap.put("sleepType",sleepArrayInfo.sleepType);//0清醒 1深睡 2浅睡
            paramMap.put("time",sleepArrayInfo.time);
            paramMap.put("affiliationDate",sleepArrayInfo.affiliationDate);
            String dataJs = getMapRJ(paramMap);
            sb.append(dataJs).append(",");
        }
        int index = sb.lastIndexOf(",");
        if (index>=0){
            sb.deleteCharAt(index);
        }
        sb.append("]");
        Map<String, Serializable> paramMap2 = new HashMap<>();
        paramMap2.put(bizId, "08");
        paramMap2.put(funcId, "40");
        paramMap2.put(version, "01");
        paramMap2.put(HttpConstant.RequestField.authKey, authKey);
        paramMap2.put(HttpConstant.RequestField.data, sb.toString());
        return getMapRJ(paramMap2);
    }




    String getBraPageJS(String authKey, int pageNo, int pageSize) {
        return getPageJS("08", "120", "01", authKey, pageNo, pageSize);
    }

    public String getNewestBraHeartTime(String authKey) {
        Map<String, Serializable> paramMap = new HashMap<>();
        paramMap.put("bizId", "08");
        paramMap.put("funcId", "121");
        paramMap.put("version", "01");
        paramMap.put("authKey", authKey);
        paramMap.put("data", "{}");
        return getMapRJ(paramMap);
    }
}
